package com.appwx.sdk.http;

import android.content.Context;
import android.text.TextUtils;

import com.appwx.sdk.app.Constants;
import com.appwx.sdk.http.AsyncAppRunner.RequestListener;
import com.appwx.sdk.model.AppParameters;
import com.appwx.sdk.model.OpenApiModel;
import com.appwx.sdk.view.WeiboException;

public class OpenApi {

	
	
	/**
 	 * getTXOpenId
 	 * @param mContext
 	 * @param mSnsModel
 	 * @param listener 
 	 * TODO 获取腾讯OpenID
 	 * @exception 
 	 * @since  1.0.0
 	*/
 	public static void getTXOpenId(Context mContext, OpenApiModel openApiModel, RequestListener listener) {
		AppParameters parameters = new AppParameters();
		parameters.add("access_token", openApiModel.getAccessToken().getToken());
		AsyncAppRunner mSnsRunner = new AsyncAppRunner(openApiModel);
		mSnsRunner.request(mContext, Constants.TENCENT_WEIBO_ACCESS_TOKEN, parameters, Utility.HTTPMETHOD_GET, listener);
	}
 	
 	/**
 	 * 分享文字
 	 * @param context 上下文
 	 * @param openApiModel 
 	 * @param param 可变参数(分享标题、文字、主要内容、链接地址、经度(lon)、纬度(lat))
 	 * @param lon 经度
 	 * @param lat 纬度
 	 * @param listener 回调监听
 	 */
 	public static void shareText(Context context,OpenApiModel openApiModel,double lon, double lat,RequestListener listener,Object ...param){
 		if(TextUtils.isEmpty(openApiModel.getAccessToken().getToken())){
 			listener.onError(new WeiboException("您的token无效或者已经过期，请重新认证!"));
 			return;
 		}
 		String url = "";
 		AppParameters parameters = new AppParameters();
 		parameters.add(OpenApiModel.TOKEN, openApiModel.getAccessToken().getToken());
 		
 		switch (openApiModel.getApp_type()) {
		case OpenApiModel.TYPE_SINA_WEIBO:
			/*
			 * 请求方式：post
			 * 请求URL：https://api.weibo.com/2/statuses/update.json
			 * 请求参数：status=text&access_token=openApiModel.getAccessToken().getToken()
			 */
			url = Constants.SINA_API_HOST + Constants.SINA_API_UPDATE;
			parameters.add("status", (String)param[2]);
			if (lon != 0) {
				parameters.add("long", String.valueOf(lon));
			}
			if (lat != 0) {
				parameters.add("lat", String.valueOf(lat));
			}
			break;
			
		case OpenApiModel.TYPE_TX_WEIBO:
			/*
			 * 请求方式：post
			 * 请求URL:https://open.t.qq.com/api/t/add
			 * 请求参数：content=text&oauth_consumer_key=OpenApiModel.getAPP_KEY()&openid=openApiModel.getAccessToken().getOpenId()&oauth_version=2.a
			 */
			url = Constants.TENCENT_WEIBO_API_HOST + Constants.TENCENT_WEIBO_ADD;
			parameters.add("content", (String)param[2]);
			parameters.add("oauth_consumer_key", OpenApiModel.getAPP_KEY());
			parameters.add("openid", openApiModel.getAccessToken().getOpenId());
			parameters.add("oauth_version", "2.a");
			if (lon != 0) {
				parameters.add("longitude", String.valueOf(lon));
			}
			if (lat != 0) {
				parameters.add("latitude", String.valueOf(lat));
			}
			break;
			
		case OpenApiModel.TYPE_TX_QQ:
			
			break;
			
		case OpenApiModel.TYPE_RENREN:
			/*
			 * 请求方式：POST
			 * 请求URL：https://api.renren.com/v2/feed/put
			 * 请求参数：access_token=openApiModel.getAccessToken().getToken()&title=text&message=text&description=text&targetUrl=text_url
			 */
			url = Constants.RENREN_API_HOST + Constants.RENREN_API_FEED;
			parameters.add("message",(String)param[1]);
			parameters.add("description",(String)param[2]);
			parameters.add("title",(String)param[0]);
			parameters.add("targetUrl",(String)param[3]);
			
			break;

		default:
			break;
		}
 		
 		AsyncAppRunner asyncAppRunner = new AsyncAppRunner(openApiModel);
 		asyncAppRunner.request(context, url, parameters, Utility.HTTPMETHOD_POST, listener);
 		
 	}
	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
}
